package com.roger.tool.utils;

/**
 * @author lch
 * @version Create time：2016-12-23 类说明:异或加密 解密
 */
public class XorEncryptUtli {

	public static final String SECRET_KEY ="soulgame";
	/**
	 * 异或处理，执行第一次加密，执行第二次解密
	 * 
	 * @param inStr
	 * @param secret
	 *            密钥(通过异或运算的某个数字或字符串)
	 * @return
	 */
	public static String xorConvert(String inStr, String secretKey) {

		char[] a = inStr.toCharArray();
		byte[] keyDatas = secretKey.getBytes();
		int keyIndex = 0;
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ keyDatas[keyIndex]);
			if (++keyIndex >= keyDatas.length) {
				keyIndex = 0;
			}
		}
		String s = new String(a);
		return s;
	}

	public static String xorConvert(String inStr, byte secretKey) {
		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ secretKey);
		}
		String s = new String(a);
		return s;
	}

}
